import { EJSON } from 'meteor/ejson';

Meteor.publish('partners',function(selector,options){
  return Partners.find(selector,options);
});
Meteor.publish('wallets',function(selector,options){
  return Wallets.find(selector,options);
});
Meteor.publish('draws',function(selector,options){
  return Draws.find(selector,options);
});
Meteor.publish('capitals',function(selector,options){
  return Capitals.find(selector,options);
});
Meteor.publish('moneyAccount',function(selector,options){
  return MoneyAccount.find(selector,options);
});
Meteor.publish('curUser',function(id){
  return Partners.find({id:parseInt(id)});
});
Meteor.publish('goods',function(selector,options){
  return goods.find(selector,options);
});
Meteor.publish('accessTokens',function(selector){
  return AccessToken.find(selector);
});

Meteor.publish('loginTokens',function(selector,options){
  return LoginToken.find(selector,options);
});
Meteor.publish('companyGoods',function(id){
  var platoon = Partners.find({parent_id:id});
  var monitorId = [];
  platoon.forEach((item) => {
    var monitor = Partners.find({parent_id:item.id});
    monitor.forEach((value)=>{
      monitorId.push(value.id);
    })
  })
  return goods.find({monitor_partner_id:{$in:monitorId}});
});

Meteor.publish('platoonGoods',function(id,ex){
  var monitorId = [];
  var monitor = Partners.find({parent_id:item.id});
  monitor.forEach((value)=>{
    monitorId.push(value.id);
  })
  return goods.find({partner_id:{$in:monitorId}},ex);
});

Meteor.publish('monitorGoods',function(id,ex){
  return goods.find({partner_id:{$in:[id]}},ex);
});

Meteor.publish('partnerGoods',function(id,ex,type){
  if(type == 'company'){
    var platoon = Partners.find({parent_id:parseInt(id)});
    var monitorId = [];
    platoon.forEach((item) => {
      var monitor = Partners.find({parent_id:parseInt(item.id)});
      monitor.forEach((value)=>{
        monitorId.push({partner_id:{$in:[parseInt(value.id)]}});
      })
    })

    return goods.find({$or:[{partner_id:12},{partner_id:37}]});
  }

  if(type == 'platoon'){
    var monitorId = [];
    var monitor = Partners.find({parent_id:parseInt(id)});
    monitor.forEach((value)=>{
      monitorId.push({partner_id:{$in:[parseInt(value.id)]}});
    })
    return goods.find({$or:monitorId},ex);
  }

  if(type == 'monitor'){
    return goods.find({partner_id:{$in:[parseInt(id)]}},ex);
  }
});

Meteor.publish('orderGoods',function(selector,options){
  return OrderGoods.find(selector,options);
});

Meteor.publish('classCodes',function(selector,options){
  return ClassCodes.find(selector,options);
});
